package com.xiaomaoguai.webkit.web.action;

import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.propertyeditors.CustomBooleanEditor;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;

import com.xiaomaoguai.user.model.User;
import com.xiaomaoguai.webkit.Constants;
import com.xiaomaoguai.webkit.web.propertiesEditor.EscapeHtmlEditor;

/**
 * @Description :
 * @FileName: BaseController.java
 * @Author :WeiHui.Zhang
 * @Data : 2015年5月9日 下午5:29:05
 * @Version:V1.00
 */
public abstract class BaseController {

	protected static final Log LOG = LogFactory.getLog(BaseController.class);

	@InitBinder
	public void initBinder(WebDataBinder binder) {
		binder.registerCustomEditor(String.class, new EscapeHtmlEditor());
		binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), true));
		binder.registerCustomEditor(boolean.class, new CustomBooleanEditor(null, null, true));
	}

	protected Integer getCurrentUser(HttpSession session) {
		User user = this.getCurrentUserInfo(session);
		return user != null ? user.getUserId() : null;
	}

	protected User getCurrentUserInfo(HttpSession session) {
		return (User) session.getAttribute(Constants.CURRENT_USER);
	}

	protected abstract String getJspPathPrefix();

	protected String getJspPathPrefix(String path) {
		return getJspPathPrefix() + path;
	}
}